/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rational.sheet.table.cell.model.format;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 *
 * @author Overmind
 */
public class NumericCellFormat
    implements CellFormat
{    
    public static final int DEFAULT_PRECISION = 2;
    public static final boolean DEFAULT_SEPARATION = false;
    
    private int precision;
    private boolean separation;
    
    public NumericCellFormat()
    {
        this(DEFAULT_PRECISION, DEFAULT_SEPARATION);
    }
    
    public NumericCellFormat(int precision, boolean separation)
    {
        this.precision = precision;
        this.separation = separation;
    }

    @Override
    public String formated(String value)
    {
        try
        {
            BigDecimal numeric = new BigDecimal(value);
            BigDecimal rounded = numeric.setScale(precision, RoundingMode.HALF_EVEN);
            
            String valueInString = rounded.toPlainString();
            
            if(separation)
            {
                
                throw new UnsupportedOperationException("Unsupported operation yet");
            }
            else
            {
                return valueInString;
            }
        }
        catch(NumberFormatException nfe)
        {
            return value;
        }
    }
    
    public int getPrecision()
    {
        return precision;
    }
    
    public boolean getSeparation()
    {
        return separation;
    }
}
